<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()" ref="dataForm">
      <el-form-item>
        <el-select v-model="dataForm.bigbayAppId" placeholder="请选择购买项目">
          <el-option
            v-for="item in projectList"
            :key="item.bigbayAppId"
            :label="item.bigbayAppName"
            :value="item.bigbayAppId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.orderStatus" placeholder="请选择订单状态">
          <el-option v-for="item in options" :key="item.val" :label="item.label" :value="item.val"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.transactionId" placeholder="请输入订单号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="search()">查询</el-button>
        <el-button @click="download()" type="primary">下载</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="transactionId" header-align="center" align="center" label="订单号"></el-table-column>
      <el-table-column prop="gmtCreate" header-align="center" align="center" label="订单时间"></el-table-column>
      <el-table-column prop="orderName" header-align="center" align="center" label="购买项目"></el-table-column>
      <el-table-column prop="sellPageItemName" header-align="center" align="center" label="购买页名称"></el-table-column>
      <el-table-column prop="courseName" header-align="center" align="center" label="购买课程"></el-table-column>
      <el-table-column prop="orderAmount" header-align="center" align="center" label="购买价格"></el-table-column>
      <el-table-column header-align="center" align="center" label="退款价格">
        <template slot-scope="scope">
          <span style="color: red;" v-if="scope.row.status!='1'">-{{ scope.row.refundAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="fromNickname" header-align="center" align="center" label="收益获得者昵称"></el-table-column>
      <el-table-column prop="fromPhone" header-align="center" align="center" label="收益获得者手机号"></el-table-column>
      <el-table-column prop="revenue" header-align="center" align="center" label="收益">
        <template slot-scope="scope">
          <span v-if="scope.row.status=='1'">{{scope.row.revenue}}</span>
          <span v-if="scope.row.status=='-1'">0</span>
          <span v-if="scope.row.status=='-2'">{{scope.row.revenue}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="订单状态">
        <template slot-scope="scope">
          <span>{{scope.row.status=='1'?'已支付':''}}</span>
          <span style="color:#f00">{{scope.row.status=='-1'?scope.row.orderType === 1?'退会员':'全部退款':''}}</span>
          <span style="color:#f00">{{scope.row.status=='-2'?'部分退款':''}}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
  </div>
</template>
<style>
.disable {
  pointer-events: none;
  color: #ccc;
  text-decoration: underline;
  margin-right: 5px;
}
.disable .el-button--text {
  color: #ccc;
}
.active {
  cursor: pointer;
}
</style>

<script>
export default {
  data() {
    return {
      options: [
        {
          val: null,
          label: "全部"
        },
        {
          val: "1",
          label: "已支付"
        },
        {
          val: "-1",
          label: "全部退款"
        },
        {
          val: "-2",
          label: "部分退款"
        }
      ],
      dataForm: {
        transactionId: "",
        bigbayAppId: "",
        orderStatus: ""
      },
      money: "",
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      projectList: []
    };
  },
  activated() {
    this.getDataList();
    this.getClassList();
  },
  methods: {
    search(){
      this.pageIndex = 1;
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let obj = {
        page: this.pageIndex,
        limit: this.pageSize
      };
      if (this.dataForm.transactionId) {
        obj.transactionId = this.dataForm.transactionId + "";
      }
      if (this.dataForm.orderStatus && this.dataForm.orderStatus != null) {
        obj.orderStatus = this.dataForm.orderStatus;
      }
      if (this.dataForm.bigbayAppId && this.dataForm.bigbayAppId != null) {
        obj.bigbayAppId = this.dataForm.bigbayAppId;
      }
      this.$http({
        url: this.$http.adornUrl("/order/orderManage/orderRevenueList"),
        method: "get",
        params: this.$http.adornParams(obj)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
          this.dataList.forEach((item,index) => {
            if (item.status == "-2") {
             item.revenue = item.revenue - item.deductRevenue;
            }
          });
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    getClassList() {
      this.$http({
        url: this.$http.adornUrl("/saleStatistic/AppInfo/list"),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          console.log(data);
          this.projectList = data.list;
        }
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(
        `确定进行[${id ? "删除" : "批量删除"}]操作?`,
        // `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/course/category/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    download() {
      let obj = {
        page: this.pageIndex,
        limit: this.pageSize
      };
      if (this.dataForm.transactionId) {
        obj.transactionId = this.dataForm.transactionId + "";
      }
      if (this.dataForm.orderStatus && this.dataForm.orderStatus != null) {
        obj.orderStatus = this.dataForm.orderStatus;
      }
      if (this.dataForm.bigbayAppId && this.dataForm.bigbayAppId != null) {
        obj.bigbayAppId = this.dataForm.bigbayAppId;
      }
      this.$http({
        url: this.$http.adornUrl("/order/orderManage/orderRevenueList/download"),
        method: "get",
        params: this.$http.adornParams(obj),
        headers: {
          'Content-Type': 'application/json; application/octet-stream'
        },
        responseType: 'arraybuffer'
      }).then(({ data }) => {
        const blob = new Blob([data], { type: 'application/vnd.ms-excel' })
        const downloadElement = document.createElement('a')
        const href = window.URL.createObjectURL(blob)
        downloadElement.href = href
        downloadElement.download = '订单收益.xls'
        document.body.appendChild(downloadElement)
        downloadElement.click()
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href) // 释放掉blob对象
      })
    }
  }
};
</script>


